<?php
namespace App\Service\Admin;

class WhitemService extends BaseService
{
    //工时子级

    public static function getOne($id, $pas)
    {
        $config_whid = $pas['config_whid'];
        $depart_id = $pas['depart_id'];
        $res = (array) self::dbConn(self::$dbYwWhItem)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            $res = [
                'id' => 0, 'config_whid' => $config_whid,  'config_title' => '',
                'wh_id' => 0, 'delay' => 0,  'delay_name' => '', 'title' => '',
                'starthour' => '', 'endhour' => '', //'detail' => '',
                'cnt_id' => 0, 'cnt_str' => '',
            ];
        }
        $configOne = (array) self::dbConn(self::$dbConfigWorkHour)->where('id', $config_whid)->first();
        $cntOne = (array) self::dbConn(self::$dbConfigWhCnt)->where('id', $config_whid)->first();
        $res['config_title'] = empty($configOne) ? '' : $configOne['title'];
//        $res['config_detail'] = empty($configOne) ? '' : $configOne['detail'];
        $res['cnt_str'] = empty($cntOne) ? '' : $cntOne['cnt'];
        if ($res['delay']) {
            $res['delay_name'] = floor($res['delay'] / 60) . '小时' . ($res['delay'] % 60) . '分钟';
        }
        $arys['data'] = $res;
        $arys['whList'] = self::dbConn(self::$dbConfigWorkHour)
            ->select('id', 'title')
            ->where('del', 0)
            //->where('depart_id', $depart_id)
            ->get();
        $arys['cntList'] = self::dbConn(self::$dbConfigWhCnt)
            ->select('id', 'cnt as title')
            ->where('del', 0)
            ->where('wh_id', $res['config_whid'])
            ->get();
        //时间
        for ($i = 8; $i <= 12; $i ++) {
            if ($i == 12) {
                $arys['hour'][] = '12:00';
            } else {
                $ii = strlen($i)<2 ? '0'.$i : $i;
                for ($j = 0; $j <= 5; $j ++) {
                    $jj = $j == 0 ? '00' : $j * 10;
                    $arys['hour'][] = $ii.':'.$jj;
                }
            }
        }
        for ($i = 13; $i <= 17; $i ++) {
            if ($i == 17) {
                $arys['hour'][] = '17:00';
            } else {
                $ii = strlen($i)<2 ? '0'.$i : $i;
                for ($j = 0; $j <= 5; $j ++) {
                    $jj = $j == 0 ? '00' : $j * 10;
                    $arys['hour'][] = $ii.':'.$jj;
                }
            }
        }
        return self::toback(0, '', $arys);
    }

    //判断时间重复选择
    public static function chongfu($id, $wh_id, $uid, $start, $end)
    {
        //var_dump($uid, $start, $end);
        $daystart = date('Y-m-d', time()) . ' 00:00:01';
        $dayend = date('Y-m-d', time()) . ' 23:59:59';
        if ($id) {
            $old01 = (array) self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('uid', $uid)
                ->where('id', '<>', $id)
                ->where('uptime', '>=', $daystart)
                ->where('uptime', '<=', $dayend)
                ->where('starthour', '>', $start)
                ->where('starthour', '<', $end)
                ->first();
            $old02 = (array) self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('uid', $uid)
                ->where('id', '<>', $id)
                ->where('uptime', '>=', $daystart)
                ->where('uptime', '<=', $dayend)
                ->where('starthour', '<=', $start)
                ->where('endhour', '>=', $end)
                ->first();
            $old03 = (array) self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('uid', $uid)
                ->where('id', '<>', $id)
                ->where('uptime', '>=', $daystart)
                ->where('uptime', '<=', $dayend)
                ->where('endhour', '>', $start)
                ->where('endhour', '<', $end)
                ->first();
            $old04 = (array) self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('uid', $uid)
                ->where('id', '<>', $id)
                ->where('uptime', '>=', $daystart)
                ->where('uptime', '<=', $dayend)
                ->where('starthour', '>', $start)
                ->where('endhour', '<', $end)
                ->first();
        } else {
            $old01 = (array) self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('uid', $uid)
                ->where('uptime', '>=', $daystart)
                ->where('uptime', '<=', $dayend)
                ->where('starthour', '>', $start)
                ->where('starthour', '<', $end)
                ->first();
            $old02 = (array) self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('uid', $uid)
                ->where('uptime', '>=', $daystart)
                ->where('uptime', '<=', $dayend)
                ->where('starthour', '<=', $start)
                ->where('endhour', '>=', $end)
                ->first();
            $old03 = (array) self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('uid', $uid)
                ->where('uptime', '>=', $daystart)
                ->where('uptime', '<=', $dayend)
                ->where('endhour', '>', $start)
                ->where('endhour', '<', $end)
                ->first();
            $old04 = (array) self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('uid', $uid)
                ->where('uptime', '>=', $daystart)
                ->where('uptime', '<=', $dayend)
                ->where('starthour', '>', $start)
                ->where('endhour', '<', $end)
                ->first();
        }
        //var_dump($old01, $old02, $old03, $old04); exit;
        if (!empty($old01) || !empty($old02) || !empty($old03) || !empty($old04)) {
            return self::toback(-2, '存在时间重叠数据');
        }
        return self::toback();
    }

    //新增
    public static function add($pas)
    {
        $whOne = (array) self::dbConn(self::$dbYwWh)
            ->where('id', $pas['wh_id'])
            ->first();
        if (empty($whOne)) {
            // return self::toback(-2, '没有数据');
        }
//        $res = self::dbConn(self::$dbYwWhItem)
//            ->where('del', 0)
//            ->where('wh_id', '<>', $pas['wh_id'])
//            ->where('title', $pas['title'])
//            ->first();
//        if (!empty($res)) {
//            return self::toback(-2, '存在重复子工时名称');
//        }
        $resDelay = self::getDelay($pas['starthour'], $pas['endhour']);
        if ($resDelay['code'] < 0) {
            return self::toback(-2, $resDelay['msg']);
        }
        $pas['delay'] = $resDelay['data']['fen'];
        $pas['addtime'] = $pas['uptime'] = toDate();
        $lastid = self::dbConn(self::$dbYwWhItem)->insertGetId($pas);
        if (!$lastid) {
            return self::toback(-1, 'error');
        }
        self::dbConn(self::$dbYwWh)->where('id', $pas['wh_id'])->update(['delay' => $pas['delay']]);
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbYwWhItem)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
//        $res = self::dbConn(self::$dbYwWhItem)
//            ->where('wh_id', '<>', $pas['wh_id'])
//            ->where('title', $pas['title'])
//            ->first();
//        if (!empty($res)) {
//            return self::toback(-2, '存在重名数据');
//        }
        $resDelay = self::getDelay($pas['starthour'], $pas['endhour']);
        if ($resDelay['code'] < 0) {
            return self::toback(-2, $resDelay['msg']);
        }
        $pas['delay'] = $resDelay['data']['fen'];
        $pas['uptime'] = toDate();
        $resUp = self::dbConn(self::$dbYwWhItem)
            ->where('id', $id)
            ->update($pas);
        if (!$resUp) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbYwWhItem)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas = [
            'del' => 1, 'deltime' => toDate(),
        ];
        $resUp = self::dbConn(self::$dbYwWhItem)
            ->where('id', $id)
            ->update($pas);
        if (!$resUp) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //获取内容
    public static function getCntList($id)
    {
        $res = self::dbConn(self::$dbConfigWhCnt)
            ->select('id', 'cnt as title')
            ->where('del', 0)
            ->where('wh_id', $id)
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        return self::toback(0, '', $res);
    }

    //内容搜索
    public static function ssCnt($wh_id, $key)
    {
        $db = self::dbConn(self::$dbConfigWhCnt)
            ->select('id', 'cnt as title')
            ->where('del', 0)
            ->where('wh_id', $wh_id);
        if ($key) {
            $db = $db->where('cnt', 'like', '%'.$key.'%');
        }
        $res = $db->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        return self::toback(0, '', $res);
    }
}